//
//  PhotoCollectionViewCell.m
//  tour
//
//  Created by 卡忙科技 on 16/6/30.
//  Copyright © 2016年 Jushi. All rights reserved.
//

#import "PhotoCollectionViewCell.h"
#import "UIImageView+WebCache.h"

@interface PhotoCollectionViewCell()<UIScrollViewDelegate>

@property (nonatomic,weak) UIScrollView *scroll;
@property (nonatomic,weak) UIImageView *imageView;

@end
@implementation PhotoCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor blackColor];
        
        UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:self.bounds];
        scroll.maximumZoomScale = 3.0f;
        scroll.minimumZoomScale = 1.0f;
        scroll.delegate = self;
        self.scroll = scroll;
        [self.contentView addSubview:scroll];
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.bounds];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        self.imageView = imageView;
        [scroll addSubview:imageView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(disMiss:)];
        [scroll addGestureRecognizer:tap];
    }
    return self;
}
- (void)setImageName:(NSString *)imageName
{
    [self.scroll setZoomScale:1 animated:NO];
    _imageName = imageName;
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:imageName] placeholderImage:GetImage(@"default_goods_image") completed:nil];
}
- (void)disMiss:(UITapGestureRecognizer *)reg
{
    if (self.dismissBlock) {
        self.dismissBlock();
    }
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    CGFloat xcenter = scrollView.center.x , ycenter = scrollView.center.y;
    xcenter = scrollView.contentSize.width > scrollView.frame.size.width ? scrollView.contentSize.width/2 : xcenter;
    ycenter = scrollView.contentSize.height > scrollView.frame.size.height ? scrollView.contentSize.height/2 : ycenter;
    self.imageView.center = CGPointMake(xcenter, ycenter);
}


@end
